<template>
  <panel title="个人中心">
    <div>管理员ID: {{ id }}</div>
    <el-divider></el-divider>
    <div>账号: {{ account }}</div>
    <el-divider></el-divider>
    <div>用户组: {{ userGroup }}</div>
    <el-divider></el-divider>
    <div>创建时间: {{ ctime }}</div>
    <el-divider></el-divider>
    <div class="img">
      <span>管理员头像: </span
      >
      <!-- name="zzz"  改变上传的文件字段名 -->
      <!-- :headers = '{ token: "xxx" }'  请求头，可以放token -->
      <!-- :data="{fail : '123'}"  请求额外的参数 -->
      <el-upload
        class="avatar-uploader"
        :on-success="success"
        :action=" BASE_URL + '/users/avatar_upload'"
        :before-upload="fileUpload"
        :show-file-list="false"  
      >
        <img v-if="img" :src=" BASE_URL + img" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
  </panel>
</template>

<script>
import axios from "@/utils/axios";
import dayjs from "dayjs";
import { getInfo, avatarEditReq } from "@/api/account.api";
import { BASE_URL } from '@/utils/common'

export default {
  data() {
    return {
      img: "",
      account: "",
      id: "",
      userGroup: "",
      ctime: "",
      fileType: ["image/png", "image/jpg", "image/jpeg"],
      BASE_URL
    };
  },
  created() {
    this.userGet();
  },
  methods: {
    //上传前
    fileUpload(file) {
      console.log(file);
      console.log("上传前停止");
      if (this.fileType.includes(file.type) && file.size / 1024 <= 100) {
        console.log('大小和格式正常');
        return true;
      } else {
        this.$message.error('图片格式或大小错误')
        return false;
      }
    },

    //上传成功时
    success(response) {
      console.log(response);
      if (response.code == 0) {
        let imgUrl = response.imgUrl.split("/").pop();
        // console.log(imgUrl);
        avatarEditReq(imgUrl).then((res) => {
          console.log(res);
          if (res.data.code == 0) {
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            this.userGet();
            this.$bus.$emit('changeImg')
          }
        });
      }
    },

    async userGet() {
      let res = await getInfo();
      // console.log(res);
      this.img = res.data.imgUrl;
      this.account = res.data.account;
      this.id = res.data.id;
      this.userGroup = res.data.userGroup;
      this.ctime = dayjs(res.data.ctime).format("YYYY/MM/DD");
    },
  },
};
</script>

<style lang="less" scoped>
img {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
}
.el-divider--horizontal {
  margin: 24px 0 !important;
}
.img {
  display: flex;
  align-items: center;
}

.avatar {
  border-radius: 50%;
}
</style>